/**
 *  @file SocketFactory.h
 */
#ifndef _SOCKETFACTORY_H_
#define _SOCKETFACTORY_H_

#include "../common/BaseObject.h"
#include "../collections/ListT.h"
#include "Socket.h"

namespace cppflib
{

class CFString;

namespace net
{

/**
 *  A structure which contains a list of virtual hosts listening on the same port
 */
class _DLLAPI VirtualHostInfo : public BaseObject
{
protected:
   u16_t listenPort;
   CFString *pBindAddr;
   collections::ListCppObjT<BaseObject> * pVirtualHostList;

protected:
   VirtualHostInfo();

public:
   VirtualHostInfo(u16_t listenPort, const CFString & bindAddr, const CFString & hostAddr);
   virtual ~VirtualHostInfo();   
   virtual bool AddVirtualHost(const CFString & hostAddr);

   u16_t GetListenPort();
   const CFString & GetBindAddr();
   const collections::ListCppObjT<BaseObject> & GetVHList();

   friend class SocketFactory;
};

/**
 *  Socket factory for normal TCP client/server
 */
class _DLLAPI SocketFactory : public BaseObject
{
private:
    void ReleaseBeforeThrowing();
    bool CheckDuplicatedPort();

protected:
    bool isServer;
    collections::ListCppObjT<VirtualHostInfo> * pVirtualHostInfoList;

public:
    SocketFactory(void);  ///< for client
    explicit SocketFactory(collections::ListCppObjT<VirtualHostInfo> * pVHInfoList); /// for server
    virtual ~SocketFactory(void);

    virtual Socket * CreateSocket(bool isSSL, const CFString &socketAddress, u16_t socketPort);
    virtual bool CreateListeningSockets(collections::ListCppObjT<Socket> &sockList, int backLog);
};


} // end of namespace net

} // end of namespace cppflib


#endif
